Ext.namespace("Task.Detail");

Task.Detail.panel= function() {
	
	this.txtTitle = new Ext.form.TextField({
        fieldLabel: '任务名称',
        width: 300,
        allowBlank: false,
        blankText: '请输入任务名称!'
    });
    
	this.txtExecutor = new Ext.form.TextField({
		fieldLabel: '执行人',
		readOnly: true,
		width: 190,
		allowBlank: false,
		blankText: '请选择执行人',
		listeners: {
		   'render': function(cmp) {
//				var self = cmp.ownerCt.ownerCt.ownerCt;
//				cmp.getEl().on('click', self.showSelector, self); 
			}
		}
	});
	this.txtTaskOrigin = new Ext.form.ComboBox({
		width: 100,
		fieldLabel: '任务来源',
        store: new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.TASKORIGINVALUE
        }),
        value: 0,
        valueField: 'value',
        displayField: 'name',
        editable: false,
        allowBlank: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	
	this.txtContent = new Ext.form.HtmlEditor({
		fieldLabel: '任务内容',
		width:420,
		height:200,
		designMode:'off',//Modified by wilson
		autoScroll:true
	});
	this.dtFeedback = new Ext.ux.form.DateTime({
		fieldLabel: '完成时间',
		timeFormat:'H:i',
        timeConfig: {
             altFormats:'H:i',
            allowBlank: true    
        },
        dateFormat:'Y-m-d',
        dateConfig: {
            altFormats:'Y-m-d',
            allowBlank: false    
       }
	});
	this.chkRemind = new Ext.form.CheckboxGroup({
		width:120,
		fieldLabel: '提醒设置',
		items: [
		      {boxLabel: '短信', name: 'smsRemind'},
		      {boxLabel: '邮件', name: 'emailRemind'}
		]
	});
	Task.Detail.panel.superclass.constructor.call(this,{
		title:'任务详情',
		labelWidth: 75,
        labelAlign: 'right',
        width: 500,
        height:350,
        frame:true,
		items:[ this.txtTitle, this.txtContent, {
			layout:"column",
			defaults: {layout: 'form'},
			items: [{
					columnWidth: .6, 
					items: [this.txtExecutor, this.dtFeedback]
				}, {
					columnWidth: .4,
					items: [this.txtTaskOrigin, this.chkRemind]
			}]
		}]
	});
};
Ext.extend(Task.Detail.panel,Ext.FormPanel,{
	initUpdate:function(data){
		this.getForm().reset();
		this.taskId = data.TASK_ID;
		this.txtTitle.setValue(data.TASK_TITLE);
		this.txtContent.setValue(data.TASK_CONTENT);
		this.dtFeedback.setValue(data.TASK_COMPLETE_DATE);
		
		
		if(data.REMIND_SET==1){
			this.chkRemind.items.items[0].setValue(true);
		}else if (data.REMIND_SET==2){
			this.chkRemind.items.items[1].setValue(true);
		}else if(data.REMIND_SET==3){
			this.chkRemind.items.items[0].setValue(true);
			this.chkRemind.items.items[1].setValue(true);
		}
		
		var conn = new Ext.data.Connection({
			url:'../task/searchTaskExes.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		});
		Utils.request(conn,{
			params:{
				taskId:this.taskId
			},
			scope:this,
			success:function(data){
/*				if (!this.scope.userSelector) {
					this.scope.userSelector = new Selector.User(this.scope, '执行人');
				}*/
				var userNames = [];
				var userData =[];
				var userId = [];
				this.scope.users =[];
				this.scope.selectUserId =[];
				
				for(var i =0;i<data.exes.length;i++){
					userNames.push(data.exes[i].USER_NAME);
					userId.push(data.exes[i].USER_ID);
					this.scope.selectUserId.push(data.exes[i].USER_ID);
					
/*					var o ={};
					o.id=data.exes[i].USER_ID;
					o.name = data.exes[i].USER_NAME;
					this.scope.users.push(o);*/
					
				}
				this.scope.txtExecutor.setValue(userNames.join(';'));
			}
		})
	}
})

Ext.namespace("Task.Feed");
Task.Feed.list = function() {
	this.exeId;
	this.taskExePercent;
	this.fb_status;
	this.to;//反馈接受人
	this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../task/searchTaskFeedBack.form?timesnap=' + Date.parse(new Date())
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			fields: ['TASK_EXECUTE_ID','USER_NAME', 'CONTENT', 'UPDATE_DATE','TASK_FEEDBACK_PERCENT','FROM_NAME','TO_NAME']
		}),listeners:{
			 beforeload :{    
	    	  	fn : function(thiz,options){
       	          Ext.apply(thiz.baseParams,
       	          { 
       	          	exeId:this.exeId
       	          });    
              },
              scope:this 
	    	}
		}
	});
	this.renderDays = function(value){
		var date = Date.parseDate(value.substring(0,10),"Y-m-d");
		var days = parseInt((date.getTime()-new Date().getTime())/(1000*60*60*24));
		if(days<0)
			return "<font color='red'>逾期"+(-days)+"天</font>";
		else
			return "<font >还剩"+days+"天</font>";
	};
	this.colModel = new Ext.grid.ColumnModel({
	    defaults: {
	        width: 100,
	        sortable: true,
	        align: 'center'
	    },
	    columns: [
			{header: 'TASK_ID', width: 50, hidden: true, dataIndex: 'TASK_ID'},
			{header: '反馈人', width: 100, dataIndex: 'FROM_NAME'},
			{header: '接受人', width: 100, dataIndex: 'TO_NAME'},
			{header: '反馈信息',width: 100, dataIndex: 'CONTENT'},
			{header: '反馈时间',width: 200, dataIndex: 'UPDATE_DATE'},
			{header: '完成百分比',hidden:true, dataIndex: 'TASK_FEEDBACK_PERCENT' ,renderer:function(value){return value+'%'}}
	    ]
	});
	this.btnAgree = new Ext.Button({
		text:'反馈',
		scope:this,
		handler:this.feedBackTask
	})
	this.gridPanel = new Ext.grid.GridPanel({
		region: 'center',
		cm:this.colModel,
		store:this.store,
		stripeRows: true, 
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
        loadMask: {msg: '加载中，请稍候...'},
        tbar:[this.btnAgree],
        bbar:new Ext.PagingToolbar({
			 pageSize: 30,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        viewConfig: { forceFit: true }
	});
	Task.Feed.list.superclass.constructor.call(this,{
		title:'任务反馈历史信息',
		layout:'border',
		items:[this.gridPanel],
		listeners:{
			render:this.queryTask,
			activate:this.queryTask
		}
	});
};
Ext.extend(Task.Feed.list,Ext.Panel,{
	initFeedBack:function(exeId,taskExePercent,to){
		this.exeId = exeId;
		this.taskExePercent= taskExePercent;
		this.to=to;
	},
	queryTask :function(exeId){
		this.store.load({
			params:{
				start : 0,
				limit : 30,
				exeId:this.exeId
			}
		})
	},
	feedBackTask:function(){
		if(!Task.ReFeedBack.win.instance)
			Task.ReFeedBack.win.instance = new Task.ReFeedBack.win();
		Task.ReFeedBack.win.instance.show();
		Task.ReFeedBack.win.instance.initDetail(this.taskExePercent,this.exeId,this.fb_status,this.to);
	}
})


Task.Feed.win = function() {
	this.tabPanel = new Ext.TabPanel({
		region:'center'
	});
	if(!Task.Detail.panel.instance)
		Task.Detail.panel.instance = new Task.Detail.panel();
	this.tabPanel.add(Task.Detail.panel.instance);
	this.tabPanel.activate(Task.Detail.panel.instance);
	if(!Task.Feed.list.instance)
		Task.Feed.list.instance = new Task.Feed.list();
	this.tabPanel.add(Task.Feed.list.instance);
	
	this.taskDownLoadPanel = new Task.DownLoad.panel();
	this.tabPanel.add(this.taskDownLoadPanel);
	
	Task.Feed.win.superclass.constructor.call(this,{
		title: '任务反馈信息',
        width: 550,
        height:420,
        resizable: true,
        autoScroll: true,
        layout:'border',
        frame:true,
        closeAction: 'hide',
        items: [this.tabPanel],
        buttons: [new Ext.Button({text:'关闭',scope:this,handler:function(){this.hide()}})],
        buttonAlign:'center'
	});
};
Ext.extend(Task.Feed.win,Ext.Window,{
	initDetail:function(data){
		Task.Detail.panel.instance.initUpdate(data);
		Task.Feed.list.instance.initFeedBack(data.TASK_EXE_ID,data.TASK_EXE_PERCENT,data.TASK_EXE_USER_ID);
		this.taskDownLoadPanel.initFeedBack(data.TASK_ID);
		this.taskDownLoadPanel.queryUpfile(data.TASK_ID);
		this.tabPanel.activate(Task.Detail.panel.instance);
	}
});



